package com.api.service;

import com.api.domain.StatisticsCaseNum;
import com.api.utils.HelperUtils;
import com.api.utils.SendMailUtil;

public class CasesExecute {

	SendMailUtil SMail = new SendMailUtil();
	HelperUtils hu = new HelperUtils();
	OperationExcel oe = new OperationExcel();
	String[] filesName = hu.getFileName();
	String statisticsData = "";

	StatisticsCaseNum statisticsCaseNum = new StatisticsCaseNum();
	int yestest = 0;
	int notest = 0;

	private void execuCase() {
		System.out.println("开始执行用例.....");
		for (int i = 0; i < filesName.length; i++) {
			statisticsCaseNum = oe.readExcel(filesName[i]);
			statisticsData += oe.getStatisticsData();
			yestest += statisticsCaseNum.getExecyestest();
			notest += statisticsCaseNum.getExecnotest();
		}

		System.out.println("用例执行完毕!");
	}

	public void execuSendMail() {
		execuCase();
		try {
			System.out.println("开始发送邮件.....");
			SMail.sendmail(getContent(statisticsData), filesName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("邮件发送成功！");
	}

	private String getContent(String statisticsData) {
		return "<html><head>" + "<style> "
				+ "<!--table.formdata{border:1px solid #5F6F7E;border-collapse:collapse;font-family:Arial;}"
				+ "table.formdata caption{text-align:center;padding-bottom:16px;}"
				+ "table.formdata th{border:1px solid #5F6F7E;"

				+ "background-color:#669999;" + "color:#000000px;"

				+ "text-align:center;font-weight:normal;padding:2px 8px 2px 6px;margin:0px;}"
				+ "table.formdata td{margin:0px;padding:0px;border:1px solid #ABABAB;}"
				+ "table.formdata input{width:100px;padding:1px 3px 1px 3px;margin:0px;"
				+ "border:none;font-family:Arial;}" + ".btn{border:1px solid #0083f2;font-family:Arial;}-->"
				+ "</style>"

				+ "</head><body>" + "<table class='formdata'>"
				+ "<caption><font size='5' color='#4876FF'>接口测试结果统计</caption>" + "<tr>" + "<th scope='col'>分类</th>"
				+ "<th scope='col'>接口</th>" + "<th scope='col'>用例数量</th>" + "<th scope='col'>执行数量</th>"
				+ "<th scope='col'>未执行数量</th>"

				+ "<th scope='col'>请求方式</th>" + "<th scope='col'>是否成功</th>"

				+ "<th scope='col'>响应时间</th>" + "<th scope='col'>平均响应时间</th>" + "<th scope='col'>最长响应时间</th>"

				+ "<th scope='col'>成功率</th>" + "<th scope='col'>失败率</th>"

				+ "</tr>" + statisticsData + "<tr>" + "<td align='center' scope='row'>总计</td>" + "<td align='center' >共"
				+ filesName.length + "个分类</td>" + "<td align='center' >" + (yestest + notest) + "</td>"
				+ "<td align='center' >" + yestest + "</td>" + "<td align='center' >" + notest + "</td>"

				+ "<td align='center' >--</td>" + "<td align='center' >--</td>"

				+ "<td align='center' >--</td>" + "<td align='center' >--</td>" + "<td align='center' >--</td>"
				+ "<td align='center' ><font color='red'>"
				+ HelperUtils.num_percent(statisticsCaseNum.getExectrue(), yestest) + "</font></td>"
				+ "<td align='center' ><font color='red'>"
				+ HelperUtils.num_percent(statisticsCaseNum.getExecfalse(), yestest) + "</font></td>"

				+ "</tr>" + "</table>" + "</body></html>" + "<br/><font color='red'>备注：详细情况请下载对应附件进行查看</font>";

	}

}
